import cv2


# ==============================
# 轮廓查找
# ==============================
def test():
    #
    src = cv2.imread('graph.png')

    # 转换颜色空间
    img = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
    # 二值化（非二值化和二值化的轮廓数量不同）
    retval, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    # 查找轮廓
    contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # 轮廓
    print(), print(">>>>>> 轮廓: ", type(contours))
    for i, contour in enumerate(contours):
        print("第 {} 个轮廓有 {} 点".format(i, len(contour)))
    # 层级
    print(">>>>>> 层级: ", type(hierarchy))
    print(hierarchy)
